package com.suxin.datahub.core.meta;

import com.suxin.datahub.database.DataBaseFactory;
import com.suxin.datahub.domain.DataSourceDomain;
import com.suxin.datahub.domain.database.ColumnInfo;
import com.suxin.datahub.domain.database.TableInfo;
import com.suxin.datahub.core.DataBaseMetaService;
import com.suxin.datahub.core.DataBaseQueryService;
import com.suxin.datahub.util.JdbcUtils;
import lombok.extern.slf4j.Slf4j;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * @Classname AbstractDataBaseMetaServiceImpl
 * @Description [ AbstractDataBaseMetaServiceImpl ]
 * @Author Tang
 * @Date 2020/11/9 17:34
 * @Created by ASUS
 */
@Slf4j
public abstract class AbstractDataBaseMetaService implements DataBaseMetaService {

    /**
     * sql层
     */
    private DataBaseQueryService sqlQuery;

    /**
     * 数据源配置类
     */
    private DataSourceDomain dataSourceDomain;

    /**
     * 连接
     */
    private Connection connection;

    public AbstractDataBaseMetaService(DataBaseQueryService dataBaseQueryService,
                                       DataSourceDomain dataSourceDomain) {
        this.sqlQuery = dataBaseQueryService;
        this.dataSourceDomain = dataSourceDomain;
        this.connection = DataBaseFactory.getDataSource(dataSourceDomain);
    }

    @Override
    public List<TableInfo> getTables(){
        String sql = this.sqlQuery.getTables();
        log.info("获取数据库表执行的SQL:{}",sql);
        List<TableInfo> list = new ArrayList<>();
        // 执行sql
        ResultSet resultSet = JdbcUtils.executeQuery(sql, this.connection, null);
        try {
            String tableSchema = this.connection.getSchema();
            while (resultSet.next()) {
                TableInfo tableInfo = new TableInfo();
                tableInfo.setColumns(null);
                tableInfo.setTableName(resultSet.getString(1));
                tableInfo.setTableSchema(tableSchema);
                list.add(tableInfo);
            }
        } catch (Exception e) {

        }finally {
            try {
                resultSet.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
        return list;
    }

    @Override
    public TableInfo getTableColumns(String tableSchema, String tableName){

        List<ColumnInfo> list = new ArrayList<>(10);

        TableInfo tableInfo = new TableInfo();
        tableInfo.setColumns(list);
        tableInfo.setTableName(tableName);
        tableInfo.setTableSchema(tableSchema);

        String sql = this.sqlQuery.getTableColumns(tableSchema, tableName);
        log.info("获取数据库列执行的SQL:{}",sql);
        // 执行sql
        ResultSet resultSet = JdbcUtils.executeQuery(sql, this.connection, null);

        try {
            while (resultSet.next()) {
                ColumnInfo columnInfo = new ColumnInfo();
                columnInfo.setTableName(resultSet.getString("tableName"));
                columnInfo.setTableSchema(tableSchema);
                columnInfo.setColumnName(resultSet.getString("columnName"));
                columnInfo.setDataType(resultSet.getString("dataType"));
                columnInfo.setIsNullable(resultSet.getString("isNullable"));
                list.add(columnInfo);
            }
        } catch (Exception e) {

        }finally {
            try {
                resultSet.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
        return tableInfo;
    }

    @Override
    public Boolean testConnection() {
        return this.connection != null;
    }
}